﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FDEE.Model
{
    /// <summary>
    /// T梁的等宽现浇桥面
    /// </summary>
    public class TDeckSlab : MemberBase
    {
        public int Width;
        public int Thickness = 80;
        public int TotalSpanLength;
        public int Cover = 40;
        public int NumHalfEJ, NumLink, NumHole;
        /// <summary>
        /// T梁现浇桥面板
        /// </summary>
        /// <param name="typeName"></param>
        /// <param name="pierNumber"></param>
        /// <param name="width">小桩号侧宽度</param>
        /// <param name="totalSpan">联长</param>
        /// <param name="numEj">半道伸缩缝数量</param>
        /// <param name="numLink">连续端道数</param>
        public TDeckSlab(string typeName, string pierNumber, int width, int totalSpan, int numEj, int numLink, int numHole)
            : base(typeName, pierNumber)
        {
            Width = width;
            TotalSpanLength = totalSpan;
            NumHalfEJ = numEj;
            NumLink = numLink;
            NumHole = numHole;
        }

        public override void SetConcreteClass()
        {
            ConcreteClass = eConcreteClass.C45;
        }

        public override void SetConcreteVolume()
        {
            ConcreteVolumn = Math.Round(Width * 1e-3 * TotalSpanLength * 1e-3 * Thickness * 1e-3, 3, MidpointRounding.AwayFromZero);
        }

        public override void SetRebarList()
        {
            #region N1
            int Lap = 680;
            int NumN1 = (int)((Width - 80) / 100) + 1;
            var SpliceRes = GetSplice(TotalSpanLength - NumHalfEJ * 340, Cover, Lap, 12000);

            int L1 = SpliceRes[0];
            int SpliceNumN1 = SpliceRes[1];
            int L2 = SpliceRes[2];

            if (SpliceNumN1 == 1)
            {
                RebarList.Add(new MARebarRecord(1, 0, "", eDB.DB10, TotalSpanLength - Cover * 2, NumN1, eShapeType.MA));
            }
            else if (SpliceNumN1 == 2)
            {
                if (L1 - Lap > Lap)
                    RebarList.Add(new MABRebarRecord(1, 1, "", eDB.DB10, L1 - Lap, Lap, NumN1, eShapeType.MAB));
                else
                    RebarList.Add(new MABRebarRecord(1, 1, "", eDB.DB10, L1 - Lap, Lap, NumN1, eShapeType.MBA));
                if (Lap > L2 - Lap)
                    RebarList.Add(new MABRebarRecord(1, 2, "", eDB.DB10, Lap, L2 - Lap, NumN1, eShapeType.MAB));
                else
                    RebarList.Add(new MABRebarRecord(1, 2, "", eDB.DB10, Lap, L2 - Lap, NumN1, eShapeType.MBA));
            }
            else
            {
                //RebarList.Add(new MABRebarRecord(1, 1, "", eDB.DB10, L1 - Lap, Lap, NumN1, eShapeType.MAB));
                //RebarList.Add(new MABRebarRecord(1, 2, "", eDB.DB10, Lap, L2 - Lap, NumN1, eShapeType.MAB));
                if (L1 - Lap > Lap)
                    RebarList.Add(new MABRebarRecord(1, 1, "", eDB.DB10, L1 - Lap, Lap, NumN1, eShapeType.MAB));
                else
                    RebarList.Add(new MABRebarRecord(1, 1, "", eDB.DB10, L1 - Lap, Lap, NumN1, eShapeType.MBA));
                if (Lap > L2 - Lap)
                    RebarList.Add(new MABRebarRecord(1, 2, "", eDB.DB10, Lap, L2 - Lap, NumN1, eShapeType.MAB));
                else
                    RebarList.Add(new MABRebarRecord(1, 2, "", eDB.DB10, Lap, L2 - Lap, NumN1, eShapeType.MBA));
                RebarList.Add(new MABCRebarRecord(1, 3, "", eDB.DB10, Lap, 12000 - Lap * 2, Lap, NumN1 * (SpliceNumN1 - 2), eShapeType.MABC));
            }

            RebarList.Add(new MARebarRecord(3, 0, "", eDB.DB12, 2100, NumN1 * NumLink, eShapeType.MA));
            RebarList.Add(new MORebarRecord(5, 0, "", eDB.DB16, NumN1 * NumHalfEJ, eShapeType.MO, 270, 124));

            #endregion

            #region N2
            int NumN2 = (int)((TotalSpanLength - 340 * NumHalfEJ - Cover * 2) / 100) + 1;
            var SpliceResN2 = GetSplice(Width, Cover, Lap, 12000);
            L1 = SpliceResN2[0];
            int SpliceNumN2 = SpliceResN2[1];
            L2 = SpliceResN2[2];

            if (SpliceNumN2 == 1)
            {
                RebarList.Add(new MARebarRecord(2, 0, "", eDB.DB10, Width - Cover * 2, NumN2, eShapeType.MA));
            }
            else if (SpliceNumN2 == 2)
            {
                if (L1 - Lap > Lap)
                    RebarList.Add(new MABRebarRecord(2, 1, "", eDB.DB10, L1 - Lap, Lap, NumN2, eShapeType.MAB));
                else
                    RebarList.Add(new MABRebarRecord(2, 1, "", eDB.DB10, L1 - Lap, Lap, NumN2, eShapeType.MBA));
                if (Lap > L2 - Lap)
                    RebarList.Add(new MABRebarRecord(2, 2, "", eDB.DB10, Lap, L2 - Lap, NumN2, eShapeType.MAB));
                else
                    RebarList.Add(new MABRebarRecord(2, 2, "", eDB.DB10, Lap, L2 - Lap, NumN2, eShapeType.MBA));
            }
            else
            {
                //RebarList.Add(new MABRebarRecord(2, 1, "", eDB.DB10, L1 - Lap, Lap, NumN2, eShapeType.MAB));
                //RebarList.Add(new MABRebarRecord(2, 2, "", eDB.DB10, Lap, L2 - Lap, NumN2, eShapeType.MAB));
                if (L1 - Lap > Lap)
                    RebarList.Add(new MABRebarRecord(2, 1, "", eDB.DB10, L1 - Lap, Lap, NumN2, eShapeType.MAB));
                else
                    RebarList.Add(new MABRebarRecord(2, 1, "", eDB.DB10, L1 - Lap, Lap, NumN2, eShapeType.MBA));
                if (Lap > L2 - Lap)
                    RebarList.Add(new MABRebarRecord(2, 2, "", eDB.DB10, Lap, L2 - Lap, NumN2, eShapeType.MAB));
                else
                    RebarList.Add(new MABRebarRecord(2, 2, "", eDB.DB10, Lap, L2 - Lap, NumN2, eShapeType.MBA));
                RebarList.Add(new MABCRebarRecord(2, 3, "", eDB.DB10, Lap, 12000 - Lap * 2, Lap, NumN2 * (SpliceNumN2 - 2), eShapeType.MABC));
            }
            #endregion

            #region N4
            Lap = 820;
            int NumN4 = (int)((TotalSpanLength - Cover * 2) / 100) + 1;
            var SpliceResN4 = GetSplice(Width, Cover, Lap, 12000);
            L1 = SpliceResN4[0];
            int SpliceNumN4 = SpliceResN4[1];
            L2 = SpliceResN4[2];

            if (SpliceNumN4 == 1)
            {
                RebarList.Add(new MARebarRecord(4, 0, "", eDB.DB12, Width - Cover * 2, 2 * NumHalfEJ, eShapeType.MA));
            }
            else if (SpliceNumN4 == 2)
            {
                if (L1 - Lap > Lap)
                    RebarList.Add(new MABRebarRecord(4, 1, "", eDB.DB12, L1 - Lap, Lap, 2 * NumHalfEJ, eShapeType.MAB));
                else
                    RebarList.Add(new MABRebarRecord(4, 1, "", eDB.DB12, L1 - Lap, Lap, 2 * NumHalfEJ, eShapeType.MBA));
                if (Lap > L2 - Lap)
                    RebarList.Add(new MABRebarRecord(4, 2, "", eDB.DB12, Lap, L2 - Lap, 2 * NumHalfEJ, eShapeType.MAB));
                else
                    RebarList.Add(new MABRebarRecord(4, 2, "", eDB.DB12, Lap, L2 - Lap, 2 * NumHalfEJ, eShapeType.MBA));
                //RebarList.Add(new MABRebarRecord(4, 1, "", eDB.DB12, L1 - Lap, Lap, 2 * NumHalfEJ, eShapeType.MAB));
                //RebarList.Add(new MABRebarRecord(4, 2, "", eDB.DB12, Lap, L2 - Lap, 2 * NumHalfEJ, eShapeType.MAB));
            }
            else
            {
                EXNumber ex = new EXNumber();
                if (L1 - Lap > Lap)
                    RebarList.Add(new MABRebarRecord(4, 1, "", eDB.DB12, L1 - Lap, Lap, 2 * NumHalfEJ, eShapeType.MAB));
                else
                    RebarList.Add(new MABRebarRecord(4, 1, "", eDB.DB12, L1 - Lap, Lap, 2 * NumHalfEJ, eShapeType.MBA));
                if (Lap > L2 - Lap)
                    RebarList.Add(new MABRebarRecord(4, 2, "", eDB.DB12, Lap, L2 - Lap, 2 * NumHalfEJ, eShapeType.MAB));
                else
                    RebarList.Add(new MABRebarRecord(4, 2, "", eDB.DB12, Lap, L2 - Lap, 2 * NumHalfEJ, eShapeType.MBA));
                //RebarList.Add(new MABRebarRecord(4, 1, "", eDB.DB12, L1 - Lap, Lap, 2 * NumHalfEJ, eShapeType.MAB));
                //RebarList.Add(new MABRebarRecord(4, 2, "", eDB.DB12, Lap, L2 - Lap, 2 * NumHalfEJ, eShapeType.MAB));
                RebarList.Add(new MABCRebarRecord(4, 3, "", eDB.DB12, Lap, 12000 - Lap * 2, Lap, 2 * NumHalfEJ * (SpliceNumN4 - 2), eShapeType.MABC));
            }
            #endregion

            RebarList.Add(new MARebarRecord(6, 0, "", eDB.DB16, 500, NumHole * 8, eShapeType.MA));
            RebarList.Sort();
        }


        int[] GetSplice(int L, int cover, int lap, int tryL1)
        {
            int Lr;
            int L1 = tryL1;
            int StandardLen = 12000;
            int LengthRaw = L - cover * 2;
            int n;
            if (LengthRaw <= StandardLen)
            {
                Lr = LengthRaw;
                n = 1;
                return new int[] { L1, n, Lr };
            }
            else
            {
                // 需要切分

                int m = (LengthRaw - (L1 - lap / 2)) / (StandardLen - lap);
                Lr = LengthRaw - (L1 - lap / 2) - (StandardLen - lap) * m + lap / 2;
                if (Lr < 3 * lap)
                {
                    if (L1 > 6000)
                    {
                        return GetSplice(L, cover, lap, L1 - 3000);
                    }
                    else
                    {
                        throw new Exception("无法配置桥面纵筋：切分不合理");
                    }

                }
                n = m + 2;
                return new int[] { L1, n, Lr };
            }

        }
    }
}
